// Copyright 2013 Ronald Schlenker, www.technewlogic.de.
// 
// This file is part of Byond.
// 
// Byond is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
// 
// Byond is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Byond.  If not, see <http://www.gnu.org/licenses/>.

using System;
using Jacobi.Vst.Framework;
using Jacobi.Vst.Framework.Plugin;
using NLog;

namespace Byond.Vst
{
	class Plugin : VstPluginWithInterfaceManagerBase, IVstPluginMidiSource
	{
		public Plugin(
			ByondPluginInfo pluginInfo)
			: base(
				pluginInfo.Name,
				pluginInfo.ProductInfo,
				pluginInfo.Category,
				pluginInfo.Capabilities,
				pluginInfo.InitialDelay,
				pluginInfo.PluginID)
		{
		}

		private readonly IVstHostRegistrar _vstHostRegistrar;
		private readonly Logger _logger;
		private readonly PluginPersistence _pluginPersistence;
		private readonly Func<VstMidi> _midiFactory;
		private readonly Func<VstAudioEngine> _audioEngineFactory;
		private readonly Func<PluginAudioProcessor> _pluginAudioProcessorFactory;
		private readonly Func<PluginParameters> _pluginParametersFactory;

		private PluginAudioProcessor _pluginAudioProcessor;
		private IVstMidiProcessor _vstMidi;
		private IVstPluginParameters _pluginParameters;

		protected override IVstPluginAudioProcessor CreateAudioProcessor(IVstPluginAudioProcessor instance)
		{
			return _pluginAudioProcessor;
		}

		protected override IVstMidiProcessor CreateMidiProcessor(IVstMidiProcessor instance)
		{
			return _vstMidi;
		}

		public override void Open(IVstHost host)
		{
			_vstHostRegistrar.RegisterVstHost(host);

			_audioEngineFactory();
			_vstMidi = _midiFactory();
			_pluginAudioProcessor = _pluginAudioProcessorFactory();
			_pluginParameters = _pluginParametersFactory();
			
			base.Open(host);
		}

		/// <summary>
		/// Always returns <b>this</b>.
		/// </summary>
		/// <param name="instance">A reference to the default instance or null.</param>
		/// <returns>Returns the default instance <b>this</b>.</returns>
		protected override IVstPluginMidiSource CreateMidiSource(IVstPluginMidiSource instance)
		{
			return this;
		}

		protected override IVstPluginPersistence CreatePersistence(IVstPluginPersistence instance)
		{
			return _pluginPersistence;
		}

		protected override IVstPluginParameters CreateParameters(IVstPluginParameters instance)
		{
			_logger.Info("CreateParameters");
			return _pluginParameters;
		}

		public int ChannelCount
		{
			get
			{
				var midiProcessor = Host.GetInstance<IVstMidiProcessor>();
				return midiProcessor != null ? midiProcessor.ChannelCount : 0;
			}
		}
	}
}